package com.ly.web.controller.king;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kingdee.bos.webapi.entity.IdentifyInfo;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.KingConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.KingOrgEntity;
import com.ly.heart.domain.MaterialEntity;
import com.ly.heart.service.IKingOrgService;
import com.ly.heart.service.IMaterialService;
import com.ly.heart.service.IMaterialTypeService;
import com.ly.heart.util.CurrentUserUtil;
import com.ly.heart.util.KingCommonUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping ("/heart/MaterialRemote")
@Slf4j
public class MaterialRemote {

    @Resource
    private IMaterialService materialInfoService;
    @Resource
    private IKingOrgService iKingOrgService;

    @ApiOperation ("同步拉取金蝶物料信息")
    @RequestMapping ("pull")
    public AjaxResult queryList () throws Exception {
        KingOrgEntity orgEntity = iKingOrgService.getOne ( new LambdaQueryWrapper<KingOrgEntity> ().eq ( KingOrgEntity :: getOrgId, CurrentUserUtil.getCompanyId () ).eq ( KingOrgEntity :: getIsDisabled, false ) );
        if ( orgEntity == null ) {
            throw new CustomException ( "请先至系统管理中配置金蝶账套！" );
        }
        IdentifyInfo identifyInfo = KingCommonUtil.getIdentifyInfo ( orgEntity );
        K3CloudApi client = new K3CloudApi ( identifyInfo );
        /**
         * 拉取字段：
         * FNumber：编码
         * FErpClsID：物料属性
         * FBaseUnitId.FNumber：基本单位
         * FSuite：套件
         * FFeatureItem：特征件子项
         * FName：物料名称
         * FSpecification：规格型号
         * FMnemonicCode：助记码
         * FMaterialGroup.FNumber：物料分组
         * FIsProduce：允许生产
         * FIsInventory：允许库存
         * FIsSale：允许销售
         * FIsAsset：允许转资产
         * FIsSubContract：允许委外
         * FIsPurchase：允许采购
         * FCategoryID.FNumber：存货类别
         * FTaxType.FNumber：税分类
         * FTaxRateId.FNumber：默认税率
         */
        String jsonData = "{\"FormId\":\"" + KingConstUtil.BD_MATERIAL + "\",\"FieldKeys\":\"FNumber,FErpClsID,FBaseUnitId.FNumber,FSuite,FFeatureItem,FName,FSpecification,FMnemonicCode,FMaterialGroup.FNumber,FIsProduce,FIsInventory,FIsSale,FIsAsset,FIsSubContract,FIsPurchase,FCategoryID.FNumber,FTaxType.FNumber,FTaxRateId.FNumber \",\"FilterString\":[],\"OrderString\":\"\",\"TopRowCount\":0,\"StartRow\":0,\"Limit\":2000,\"SubSystemId\":\"\"}";
        List<List<Object>> lists = client.executeBillQuery ( jsonData );
        List<MaterialEntity> materialEntities = materialInfoService.list ( new LambdaQueryWrapper<MaterialEntity> ().eq ( MaterialEntity :: getIsUsed, ConstUtil.IS_USED_YES ) );
        List<String> materialCodes = materialEntities.stream ().map ( MaterialEntity :: getMaterialCode ).collect ( Collectors.toList () );
        for ( List<Object> list : lists ) {
            log.info ( "物料->> {}", list );
            if ( ! materialCodes.contains ( list.get ( 0 ).toString () ) ) {
                MaterialEntity materialEntity = new MaterialEntity ();
                materialEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
                materialEntity.setMaterialCode ( list.get ( 0 ).toString () );
                materialEntity.setMaterialProperty ( list.get ( 1 ) == null ? null : list.get ( 1 ).toString () );
                materialEntity.setUnit ( list.get ( 2 ) == null ? null : list.get ( 2 ).toString () );
                materialEntity.setKit ( list.get ( 3 ) == null ? null : list.get ( 3 ).toString () );
                materialEntity.setSubItems ( list.get ( 4 ) == null ? null : list.get ( 4 ).toString () );
                materialEntity.setMaterialName ( list.get ( 5 ) == null ? null : list.get ( 5 ).toString () );
                materialEntity.setModel ( list.get ( 6 ) == null ? null : list.get ( 6 ).toString () );
                materialEntity.setMnemonicCode ( list.get ( 7 ) == null ? null : list.get ( 7 ).toString () );
                materialEntity.setMaterialGroup ( list.get ( 8 ) == null ? null : list.get ( 8 ).toString () );
                Object o = list.get ( 9 );
                if ( ! ObjectUtils.isEmpty ( o ) ) {
                    Boolean b = (Boolean) o;
                    materialEntity.setAllowProduce ( b == true ? "1" : "0" );
                }
                //允许库存
                o = list.get ( 10 );
                if ( ! ObjectUtils.isEmpty ( o ) ) {
                    Boolean b = (Boolean) o;
                    materialEntity.setAllowInventory ( b == true ? "1" : "0" );
                }
                //允许销售
                o = list.get ( 11 );
                if ( ! ObjectUtils.isEmpty ( o ) ) {
                    Boolean b = (Boolean) o;
                    materialEntity.setAllowSale ( b == true ? "1" : "0" );
                }
                //允许转资产
                o = list.get ( 12 );
                if ( ! ObjectUtils.isEmpty ( o ) ) {
                    Boolean b = (Boolean) o;
                    materialEntity.setAllowAsset ( b == true ? "1" : "0" );
                }
                //允许委外
                o = list.get ( 13 );
                if ( ! ObjectUtils.isEmpty ( o ) ) {
                    Boolean b = (Boolean) o;
                    materialEntity.setAllowOutsource ( b == true ? "1" : "0" );
                }
                //允许采购
                o = list.get ( 14 );
                if ( ! ObjectUtils.isEmpty ( o ) ) {
                    Boolean b = (Boolean) o;
                    materialEntity.setAllowPurchase ( b == true ? "1" : "0" );
                }
                //存货类别
                materialEntity.setInventoryType ( list.get ( 15 ) == null ? null : list.get ( 15 ).toString () );
                //税分类
                materialEntity.setTaxType ( ObjectUtils.isEmpty ( list.get ( 16 ) ) ? null : list.get ( 16 ).toString () );
                //默认税率
                materialEntity.setDefaultTaxRate ( ObjectUtils.isEmpty ( list.get ( 17 ) ) ? null : list.get ( 17 ).toString () );
                materialEntity.setCreateTime ( DateUtils.getNowDate () );
                materialInfoService.save ( materialEntity );
            }
        }
        return AjaxResult.success ();
    }

}
